package grades

import (
	"fmt"
	"sync"
)

// Students 所有学生类型
type Students []Student

// 全局变量
var (
	students      Students
	studentsMutex sync.Mutex
)

// GetByID 根据ID查找学生
func (ss Students) GetByID(id int) (*Student, error) {
	for _, s := range ss {
		if s.ID == id {
			return &s, nil
		}
	}
	return nil, fmt.Errorf("student with ID %d not found", id)
}

// Student 学生
type Student struct {
	ID        int
	FirstName string
	LastName  string
	Grades    []Grade
}

// Average 求成绩的平均值
func (s *Student) Average() float32 {
	var result float32
	for _, grade := range s.Grades {
		result += grade.Score
	}
	return result / float32(len(s.Grades))
}

// Grade 成绩
type Grade struct {
	Title string
	Type  GradeType
	Score float32
}

// GradeType 成绩类型
type GradeType string

// 成绩类型的枚举值
const (
	GradeQuiz = GradeType("Quiz")
	GradeTest = GradeType("Test")
	GradeExam = GradeType("Exam")
)
